package com.aiti.lulian.controller.crad;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.CardHolder;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.service.CardHolderService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/card/holder")
public class CardHolderController {

    private final CardHolderService cardHolderService;

    private final ILuLianEnterpriseUserService enterpriseUserService;

    @PostMapping("/saveCardHolderDate")
    public Message saveCardHolderDate(@RequestBody Map<String,String> params){
        String from = params.get("from");
        String to = params.get("to");

        if(StringUtils.isBlank(from) || StringUtils.isBlank(to)){
            return Message.fail("from and to require not null");
        }

        List<CardHolder> list = new ArrayList<>();
        List<CardHolder> list1 = cardHolderService.list(new LambdaQueryWrapper<CardHolder>().eq(CardHolder::getBaseUserId, from).eq(CardHolder::getCardHolderUserId, to));
        if (list1.size() == 0) {
            CardHolder cardHolder1 = new CardHolder();
            cardHolder1.setBaseId(IdUtil.simpleUUID());
            cardHolder1.setBaseCreateTime(DateUtil.date());
            cardHolder1.setCardHolderUserId(to);
            cardHolder1.setBaseUserId(from);
            list.add(cardHolder1);
        }

        List<CardHolder> list2 = cardHolderService.list(new LambdaQueryWrapper<CardHolder>().eq(CardHolder::getBaseUserId, to).eq(CardHolder::getCardHolderUserId, from));
        if (list2.size() == 0) {
            CardHolder cardHolder2 = new CardHolder();
            cardHolder2.setBaseId(IdUtil.simpleUUID());
            cardHolder2.setBaseCreateTime(DateUtil.date());
            cardHolder2.setCardHolderUserId(from);
            cardHolder2.setBaseUserId(to);
            list.add(cardHolder2);
        }

        cardHolderService.saveBatch(list);
        return Message.success();
    }

    /**
     * 查询所传入的用户的企业
     * @param userId
     * @return
     */
    @PostMapping("/selectUserIdToEnterpriseName")
    public Message selectUserIdToEnterpriseName(@RequestBody Set<String> userId){
        List<LuLianEnterpriseUser> list = enterpriseUserService.selectUserIdToEnterpriseName(userId);
        if(list != null && list.size() > 0){
            Map<String, String> collect = list.stream().collect(Collectors.toMap(k -> k.getUserId(), v -> v.getEnterpriseName()));
            return Message.success(collect);
        }
        return Message.success();
    }

    @ApiOperation("查询转发后的名片是否被收藏")
    @GetMapping("/isCollected")
    public Message getIsCollected(@RequestParam("userHolderCardId") String userHolderCardId, @RequestParam("userCardId") String userCardId){
        return cardHolderService.getIsCollected(userHolderCardId, userCardId);
    }
}
